<template>
  <div class="setting">
    <!-- card -->
    <el-card>
      <!-- tabs -->
      <el-tabs v-model="activeName">
        <el-tab-pane label="角色管理" name="first">
          <!-- 1 - 按钮 -->
          <el-button
            size="medium"
            type="primary"
            icon="el-icon-plus"
            @click="isShow = true"
          >新增角色</el-button>
          <!-- 1 - table -->
          <el-table
            :data="tableData"
            style="width: 100%"
            :default-sort="{ prop: 'date', order: 'descending' }"
            border
          >
            <el-table-column type="index" label="序号" width="50" sortable />
            <el-table-column prop="name" label="角色名" sortable />
            <el-table-column prop="description" label="描述" />
            <el-table-column fixed="right" label="操作">
              <!-- 通过 slot自定义结构  如果要获取到这一行的数据 可以通过 .row属性来获取 -->
              <template #default="{ row }">
                <el-button
                  type="text"
                  size="medium"
                  @click="handleClick(row)"
                >分配权限</el-button>
                <el-button
                  type="text"
                  size="medium"
                  @click="enterEdit(row)"
                >修改</el-button>
                <el-popconfirm
                  title="您确定删除此角色吗？"
                  @onConfirm="onConfirm(row)"
                >
                  <el-button
                    slot="reference"
                    type="text"
                    size="medium"
                    style="margin-left: 10px"
                  >
                    删除</el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-pagination
            :current-page="pape"
            :page-sizes="[1, 2, 3, 4]"
            :page-size="papesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="second">
          <!--form表单 -->
          <ComInfo />
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 弹框 -->
    <SetDialog
      :visible="isShow"
      :form="form"
      @update:visible="isShow = $event"
      @getData="getData"
      @clear="clearHandler"
    />
    <!-- 分配权限弹框 -->
    <PerDialog ref="perDialog" />
  </div>
</template>

<script>
// 导入form表单组件
import ComInfo from './components/ComInfo.vue'
// 导入弹框
import SetDialog from './components/SetDialog.vue'
// 导入分配权限弹框
import PerDialog from './components/perDialog.vue'

// 导入接口
import { getRoles, getRoleById, deleteRole } from '@/api/setting'
export default {
  name: 'Setting',
  components: {
    ComInfo,
    SetDialog,
    PerDialog
  },
  data() {
    return {
      // 是否弹框
      isShow: false,
      activeName: 'first',
      pape: 1, // 页码
      papesize: 4, // 页容量
      total: 1, // 总条数
      tableData: [], // 数据数组
      form: {
        name: '',
        description: ''
      }
    }
  },
  async created() {
    this.getData()
  },
  methods: {
    // 删除
    async onConfirm(row) {
      const res = await deleteRole(row.id)
      // console.log(res);
      // 重新获取数据
      this.getData()
      this.$message.success('删除角色成功')
    },
    // 清空表单
    clearHandler() {
      this.form = {
        name: '',
        description: ''
      }
    },
    // 编辑
    async enterEdit(row) {
      // console.log(row)
      const res = await getRoleById(row.id)
      // console.log(res)
      this.form = res
      // 弹框
      this.isShow = true
    },
    // 获取数据
    async getData() {
      const res = await getRoles({
        page: this.pape,
        pagesize: this.papesize
      })
      // console.log(res)
      this.tableData = res.rows
      // 保存total
      this.total = res.total
    },
    // table内部点击
    handleClick(row) {
      this.$refs.perDialog.getDataAndShow(row.id)
    },
    // 页容量改变
    handleSizeChange(val) {
      this.papesize = val
      // 一般会返回第一页
      this.pape = 1
      // 重新获取数据
      this.getData()
    },
    // 页码改变
    handleCurrentChange(val) {
      this.pape = val
      this.getData()
    }
  }
}
</script>

<style lang="scss">
.setting {
  .el-form-item__content {
    width: 400px;
  }
  .el-pagination {
    text-align: right;
  }
  .el-tab-pane {
    padding-top: 20px;
  }
  #pane-first {
    padding: 20px 50px;
  }
  .el-pagination,
  .el-table--border {
    margin-top: 20px;
  }
}
</style>
